package com.mbc.web.common.client;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;

public class ViewUtils {
	
	public static native void startAjaxLoader(String message)/*-{
		var $_span = $wnd.$("<span/>").text(message).append($wnd.$("<i/>").addClass("fa fa-spinner fa-2x fa-spin"));
		var $_divLoader = $wnd.$("<div/>").attr("id","ajaxLoader").addClass("alert alert-info text-center").append($_span);
		var $_bg = $wnd.$("<div/>").attr("id","alert-background").append($_divLoader);
		if($wnd.$("body").css("padding-left") === "0px"){
			$_bg.css("margin-left","0px");
		}
		$_bg.appendTo("body");
	}-*/;
	
	public static native void stopAjaxLoader()/*-{
		$wnd.$("#alert-background").remove();
	}-*/;
	
	public static native void showInfoMessage(String message)/*-{
		var $_span = $wnd.$("<span/>").text(message).append($wnd.$("<i/>").addClass("fa fa-exclamation"));
		$wnd.$("<div/>").attr("id","msg-box").addClass("alert alert-info text-center").append($_span).appendTo("#content_div");
		$wnd.setTimeout(function () { $wnd.$("#msg-box").fadeOut(function(){$wnd.$("#msg-box").remove();}); }, 5000);
	}-*/;
	
	public static native void showErrorMessage(String message)/*-{
		$wnd.$("#msg-box").remove();
		var $_span = $wnd.$("<span/>").text(message).append($wnd.$("<i/>").addClass("fa fa-exclamation"));
		$wnd.$("<div/>").attr("id","msg-box").addClass("alert alert-danger text-center").append($_span).appendTo("#content_div");
		//$wnd.setTimeout(function () { $wnd.$("#msg-box").remove(); }, 5000);
	}-*/;
	
	public static native void showSuccessMessage(String message)/*-{
		var $_span = $wnd.$("<span/>").text(message).append($wnd.$("<i/>").addClass("fa fa-exclamation"));
		$wnd.$("<div/>").attr("id","msg-box").addClass("alert alert-success text-center").append($_span).appendTo("#content_div");
		$wnd.setTimeout(function () { $wnd.$("#msg-box").fadeOut(function(){$wnd.$("#msg-box").remove();}); }, 5000);
	}-*/;
	
	public static native void showWarningMessage(String message)/*-{
		var $_span = $wnd.$("<span/>").text(message).append($wnd.$("<i/>").addClass("fa fa-exclamation"));
		$wnd.$("<div/>").attr("id","msg-box").addClass("alert alert-warning text-center").append($_span).appendTo("#content_div");
		$wnd.setTimeout(function () { $wnd.$("#msg-box").fadeOut(function(){$wnd.$("#msg-box").remove();}); }, 5000);
	}-*/;
	
	public static native void closeMessageBox()/*-{
		$wnd.$("#msg-box").remove();
	}-*/;
	
	public static native void showPopup(String message, int left, int top)/*-{
		var l_popup = "<div class='popover fade top in' style='display: block; top: " + top + "px; left: " + left + "px;'><div class='arrow'></div><h3 class='popover-title' style='display: none;'></h3><div class='popover-content'>" + message + "</div></div>";
		$wnd.$(l_popup).appendTo("body");
	}-*/;
	
	public static native void clearPopups()/*-{
		$wnd.$(".popover").remove();
	}-*/;
	
	private static CloseHandler<PopupPanel> confirmCloseHandler = null; 
	private static boolean alert_yes = false;
	
	public static boolean isAlertYes(){
		return alert_yes;
	}
	
	public static void showConfirm(String message, CloseHandler<PopupPanel> closeHandler){
		alert_yes = false;
		confirmCloseHandler = closeHandler;
		jsShowConfirm(message);
	}
	
	private static void confirmClose(){
		if(confirmCloseHandler != null)
			confirmCloseHandler.onClose(null);
	}
	
	private static native void jsShowConfirm(String message)/*-{
		var l_alert = "<div class='alert alert-danger modal-sm alert-confirm fade in'>" +
      					"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
  						"<h4>Oh snap! You got an error!</h4>" +
      					"<p>" + message + "</p>" +
      					"<p class='text-right'>" +
        				"<button type='button' class='btn btn-danger' id='alert-yes'>Yes</button>&nbsp&nbsp" +
        				"<button type='button' class='btn btn-default' id='alert-no'>No</button>" +
      					"</p>" +
    					"</div>";
    	$wnd.$("<div/>").attr("id","alert-background").append(l_alert).appendTo("body");
		$wnd.$("#alert-yes").click(function(){
			@com.mbc.web.common.client.ViewUtils::alert_yes = true;
			$wnd.$(".alert").alert('close');
		});
		$wnd.$("#alert-no").click(function(){
			@com.mbc.web.common.client.ViewUtils::alert_yes = false;
			$wnd.$(".alert").alert('close');
		});
		$wnd.$('.alert').bind('closed.bs.alert', function () {
			$wnd.$("#alert-background").remove();
		  	@com.mbc.web.common.client.ViewUtils::confirmClose()();
		})
	}-*/;
	
	public static native void attachBlurEvent()/*-{
		$wnd.$("#msg-box").remove();
		$wnd.$("input[required]").blur(function(){
            var l_parent = $wnd.$(this).parent();      
            if($wnd.$(this).val()==="")
            {
                if(!l_parent.hasClass("has-error")){
                    l_parent.addClass("has-error has-feedback")
                        .append($wnd.$("<i/>").addClass("fa fa-exclamation-circle form-control-feedback"));
                }
            }
            else{
                l_parent.removeClass("has-error has-feedback").children("i").remove();
            }
        });
	}-*/;
	
	public static native void prepareTabindexNEnter()/*-{
		$wnd.$("[tabindex]").keydown(function(e){
			if(e.keyCode === 13){
				var l_index = $wnd.parseInt($wnd.$(this).attr("tabindex"));
				$wnd.$("[tabindex=" + (l_index + 1) + "]").focus();
			}
		});
	}-*/;
	
	public static native void scrollerbar()/*-{
		var l_parentWidht = $wnd.$('.table-scroll').parent().width();
		$wnd.$('.table-scroll').perfectScrollbar();
        $wnd.$('.table-scroll').width(l_parentWidht).perfectScrollbar('update');
	}-*/;
	
	public static native void clearRequiredNotifications()/*-{
		$wnd.$("input[required]").parent().removeClass("has-error has-feedback").children("i").remove();
	}-*/;
	
	public static native void executeAccessControl(String accessCodes)/*-{
		var l_accessCodes = accessCodes.split(',');
		$wnd.$("[data-ac]").each(function(){
			var l_ac = $wnd.$(this).data("ac");
			if(l_ac !== ""){
				var l_flag = false;
				var l_acs = l_ac.split(',');
				for(var i = 0; i < l_acs.length; i++){
					if(accessCodes.indexOf(l_acs[i]) > -1){
						l_flag = true;
						break;
					}
				}
				if(!l_flag)
					$wnd.$(this).hide();
			}
		});
	}-*/;
	
	public static native boolean isValidate()/*-{
		$wnd.$("#msg-box").remove();
		var l_required_fields = $wnd.$("input[required]").each(function(){
            if($wnd.$(this).val()==="")
            {
                var l_parent = $wnd.$(this).parent();
                if(!l_parent.hasClass("has-error")){
                    l_parent.addClass("has-error has-feedback")
                        .append($wnd.$("<i/>").addClass("fa fa-exclamation-circle form-control-feedback"));
                }
            }
        });
        for(i = 0; i < l_required_fields.length; i++)
        {
            if($wnd.$(l_required_fields[i]).val() === "")
            {
                $wnd.$(l_required_fields[i]).focus();
                return false;
            }
        }
        return true;
	}-*/;
	
	public static KeyPressHandler numberKeyPressHandler = new KeyPressHandler() {
		
		@Override
		public void onKeyPress(KeyPressEvent event) {
			if(event.getNativeEvent().getKeyCode() != KeyCodes.KEY_BACKSPACE && 
					event.getNativeEvent().getKeyCode() != KeyCodes.KEY_DELETE &&
					event.getNativeEvent().getKeyCode() != KeyCodes.KEY_TAB ){
				if(!Character.isDigit(event.getCharCode())){
					((TextBox)event.getSource()).cancelKey();
				}
			}
		}
	};
	
	public static ChangeHandler numberChangeHandler = new ChangeHandler() {
		
		@Override
		public void onChange(ChangeEvent event) {
			TextBox l_txt = (TextBox)event.getSource();
			if(l_txt.getText().equals(""))
				l_txt.setText("0");
		}
	};
}
